#include "GY_GraphicalObject.h"
#include <QDebug>

GY_GraphicalObject::GY_GraphicalObject(const QString &guid, QGraphicsItem *parent) : GY_ContainerTransform(guid,this, parent)
{
    initializationPropertyInfo();
    this->setCenterPointF(m_PropertyInfoMap["Position"].propertyValue.toPointF());
}

bool GY_GraphicalObject::addAttribute(const QString &name, const QVariant &value)
{
    if(m_PropertyInfoMap.contains(name)) {
        return false;
    }
    GY_PropertyInfo Attribute(this);
    Attribute.propertyName = name;
    Attribute.propertyValue = value;
//    Attribute.updateType = ;
//    Attribute.propertyType = ;
    Attribute.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_GraphicalObject::updateAttribute;
    m_PropertyInfoMap.insert(name,Attribute);
    m_PropertyInfoMap[Attribute.propertyName].setConnect();
    return true;
}

bool GY_GraphicalObject::deleteAttribute(const QString &name, const QVariant &value)
{
    if(!m_PropertyInfoMap.contains(name)) {
        return false;
    }
    m_PropertyInfoMap.remove(name);
    return true;
}

void GY_GraphicalObject::setContainerEditPointF()
{
    setEditPointF("leftTop",QPointF(this->childrenBoundingRect().x(),this->childrenBoundingRect().y()));
    setEditPointF("rightTop",QPointF(this->childrenBoundingRect().x()+this->childrenBoundingRect().width()
                                     ,this->childrenBoundingRect().y()));
    setEditPointF("leftBottom",QPointF(this->childrenBoundingRect().x()
                                       ,this->childrenBoundingRect().y()+this->childrenBoundingRect().height()));
    setEditPointF("rightBottom",QPointF(this->childrenBoundingRect().x() + this->childrenBoundingRect().width()
                                          ,this->childrenBoundingRect().y() + this->childrenBoundingRect().height()));
    setEditPointF("RotationPoint",QPointF(this->childrenBoundingRect().x()+this->childrenBoundingRect().width()/2
                                   ,this->childrenBoundingRect().y()+this->childrenBoundingRect().height()/2));
}

void GY_GraphicalObject::initializationPropertyInfo()
{
    GY_PropertyInfo ObjectName(this);
    ObjectName.propertyName = "ObjectName";
    ObjectName.propertyValue = QString();
    ObjectName.updateType = Update|OrdinaryAttributes;
    ObjectName.propertyType = QStringPropertyType;
    ObjectName.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_GraphicalObject::updateObjectName;
    m_PropertyInfoMap.insert("ObjectName",ObjectName);
    m_PropertyInfoMap[ObjectName.propertyName].setConnect();

    //位置
    GY_PropertyInfo positonPropertyInfo(this);
    positonPropertyInfo.propertyName="Position";
    positonPropertyInfo.propertyValue=QPointF(0,0);
    positonPropertyInfo.updateType = Update|MoveUpdate|PointOfRelative;
    positonPropertyInfo.propertyType = PointPropertyType;
    positonPropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_GraphicalObject::updatePosition;
    m_PropertyInfoMap.insert(positonPropertyInfo.propertyName,positonPropertyInfo);
    m_PropertyInfoMap[positonPropertyInfo.propertyName].setConnect();

}

bool GY_GraphicalObject::updatePosition(const QString &name, const QVariant &value)
{
    if(!value.canConvert<QPointF>())
    {
        return false;
    }
    //setpos设置为父级坐标
    QPointF point = value.toPointF();
    if(this->parentItem()) {
        point = this->mapToParent(this->mapFromScene(value.toPointF()));
    }
    this->setPos(point);
    m_PropertyInfoMap[name].propertyValue=this->mapFromScene(value.toPointF());
    return true;
}

bool GY_GraphicalObject::updateAttribute(const QString &name, const QVariant &value)
{
    m_PropertyInfoMap[name].propertyValue=value;
    return true;
}
